#include "board.h"

/// @brief printf重定向
/// @param ch 
/// @param f 
/// @return 
int fputc(int ch, FILE *f)
{
    USART_SendData_8bit(DEBUG_USARTx, (uint8_t)ch);

    while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);

    return ch;
}

/// @brief 系统时钟配置
/// @param  
static void RCC_Configuration(void)
{
    //SYSCLK = HSI = 8MHz = HCLK = PCLK
    RCC_HSI_Enable(RCC_HSIOSC_DIV6);  

    RCC_HCLKPRS_Config(RCC_HCLK_DIV1);
    RCC_PCLKPRS_Config(RCC_PCLK_DIV1);

    RCC_PLL_Enable(RCC_PLLSOURCE_HSI, 8000000, 8); //倍频

    //切换时钟源必要步骤
    __RCC_FLASH_CLK_ENABLE();
    FLASH_SetLatency(FLASH_Latency_3);

    RCC_SysClk_Switch(RCC_SYSCLKSRC_PLL);  //系统时钟源切换
    // RCC_SystemCoreClockUpdate(64000000);   //用处未知
}

// /// @brief 1ms定时器, 作为系统时基
// /// @param  
// static void SysTIM_Init(void)
// {
//     BTIM_TimeBaseInitTypeDef BTIM_TimeBaseInitStruct;

//     __RCC_BTIM_CLK_ENABLE();

//     __disable_irq();
//     NVIC_EnableIRQ(BTIM1_IRQn);
//     __enable_irq();

//     BTIM_TimeBaseInitStruct.BTIM_Mode = BTIM_Mode_TIMER;
//     BTIM_TimeBaseInitStruct.BTIM_Period = 999;
//     BTIM_TimeBaseInitStruct.BTIM_Prescaler = BTIM_PRS_DIV64;

//     BTIM_TimeBaseInit(CW_BTIM1, &BTIM_TimeBaseInitStruct);
//     BTIM_ITConfig(CW_BTIM1, BTIM_IT_OV, ENABLE);
//     BTIM_Cmd(CW_BTIM1, ENABLE);
// }

/// @brief debug串口初始化
/// @param  
static void DebugUartInit(void)
{
    //外设时钟使能
    RCC_AHBPeriphClk_Enable(DEBUG_USART_GPIO_CLK, ENABLE);
    DEBUG_USART_APBClkENx(DEBUG_USART_CLK, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;

    //UART TX RX 复用
    DEBUG_USART_AFTX();
    DEBUG_USART_AFRX();

    GPIO_InitStructure.Pins = DEBUG_USART_TX_GPIO_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = DEBUG_USART_RX_GPIO_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
    GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);

    //uart config
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = DEBUG_USART_BaudRate;
    USART_InitStructure.USART_Over = USART_Over_16;
    USART_InitStructure.USART_Source = USART_Source_PCLK;
    USART_InitStructure.USART_UclkFreq = DEBUG_USART_UclkFreq;
    USART_InitStructure.USART_StartBit = USART_StartBit_FE;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No ;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(DEBUG_USARTx, &USART_InitStructure);

    //优先级，无优先级分组
    NVIC_SetPriority(DEBUG_USARTx_IRQ, 0);
    //UARTx中断使能
    NVIC_EnableIRQ(DEBUG_USARTx_IRQ);
    USART_ITConfig(DEBUG_USARTx, USART_IT_RC, ENABLE);
}

/// @brief 板级初始化
/// @param  
void BoardInit(void)
{
    RCC_Configuration();
    DebugUartInit();
    InitTick(64000000);  //启用SysTimer获取系统运行时间
    // SysTIM_Init();
}





